var fs = require("fs");
var reactTools = require('react-tools');
var UglifyJS = require("uglify-js");
function insertDataStreamStore(jsText) {
    var reactClassPos = jsText.indexOf("React.createClass({");
    if(reactClassPos > -1) {
        var insertStartPos = reactClassPos + "React.createClass({".length;
        var headJsText = jsText.substring(0, insertStartPos);
        var footJsText = jsText.substring(insertStartPos, jsText.length);
        jsText = headJsText + "valueLinked: function(attr, id) {if(typeof $ == 'undefined'){return};var self = this;var timer = null;if(!$('#' + id).isBind) {$('#' + id).isBind = true;var val = '';setInterval(function() {if($('#' + id).length > 0) {if(!timer) {timer = setInterval(function () {clearInterval(timer);$('#' + id).on('keyup change', function () {val = $('#' + id).val();self.state.data[attr] = $('#' + id).val();}); }, 0);}}}, 0);}},\n\tapply: function() {\n\t\tthis.setState(this.state);\n\t}," + footJsText;
    }
    return jsText;
}
function sendPageData(req, res) {
    var url = "public" + req.url;
    var tplName = req.url.split("/")[2];
    var fileName = url.split("/")[3].substring(0, url.split("/")[3].indexOf(".js"));
    var tplUrl = "views/tpl/" + tplName + "/" + fileName + ".html";
    var jsText = cache.get(url);
    if(!jsText) {
        fs.readFile(url, function (error, jsText) {
            jsText = jsText.toString();
            fs.readFile(tplUrl, function (error, tplText) {
                if(error) {
                    tplText = "";
                }
                jsText = replaceAll(jsText, "~" + fileName, tplText.toString());
                jsText = insertDataStreamStore(jsText);
                jsText = reactTools.transform(jsText, null);//后台编译js文件
                if(isUseGlobalCache) {
                    jsText = UglifyJS.minify(jsText).code;//压缩js文件
                }
                cache.set(url, jsText, 10000);
                res.end(jsText);
            });
        });
    } else {
        res.end(jsText);
    }
}
function sendWidgetData(req, res) {
    var url = "public" + req.url;
    var tplName = req.url.split("/")[3];
    var fileName = url.split("/")[4].substring(0, url.split("/")[4].indexOf(".js"));
    var tplUrl = "public/javascripts/widget/" + tplName + "/" + fileName + ".html";
    var jsText = cache.get(url);
    if(!jsText) {
        fs.readFile(url, function (error, jsText) {
            jsText = jsText.toString();
            fs.readFile(tplUrl, function (error, tplText) {
                if(error) {
                    tplText = "";
                }
                jsText = replaceAll(jsText, "~" + fileName, tplText.toString());
                jsText = reactTools.transform(jsText, null);//后台编译js文件
                if(isUseGlobalCache) {
                    jsText = UglifyJS.minify(jsText).code;//压缩js文件
                }
                cache.set(url, jsText, 10000);
                res.end(jsText);
            });
        });
    } else {
        res.end(jsText);
    }
}
function sendServiceData(req, res) {
    var url = "public" + req.url;
    var jsText = cache.get(url);
    if(!jsText) {
        fs.readFile(url, function (error, jsText) {
            jsText = jsText.toString();
            if(isUseGlobalCache) {
                jsText = UglifyJS.minify(jsText).code;//压缩js文件
            }
            cache.set(url, jsText, 10000);
            res.end(jsText);
        });
    } else {
        res.end(jsText);
    }
}
exports.assetsAction = function(req, res) {
    if(req.url.indexOf(".css") > -1) {
        var url = "public" + req.url;
        fs.readFile(url, function(error, cssText) {
            res.end(cssText.toString());
        });
        return;
    }
    if(req.url.indexOf("widget") != -1) {
        sendWidgetData(req, res);
    } else if(req.url.indexOf("Service.js") != -1) {
        sendServiceData(req, res);
    } else {
        sendPageData(req, res);
    }

};